package soft.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import soft.pojo.ErrorGrade;
import soft.pojo.Grade;
import soft.pojo.Student;
import soft.service.ErrorGradeService;
import soft.service.GradeService;
import soft.service.StudentService;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentControl {
	@Autowired
	private StudentService studentService;
	@Autowired
	private GradeService gradeService;
	@Autowired
	private ErrorGradeService errorGradeService;

	//查询个人信息
	@RequestMapping(value = "/getself")
	public String getSelf(ModelMap modelMap, HttpSession session){
		System.out.println(session.getAttribute("aId"));
		//得强制转化为String
		Student bean = studentService.getById((String) session.getAttribute("aId"));
//          modelMap.addAttribute("adminmsg","bean");
		modelMap.addAttribute("studentmsg",bean);
		return "selectStudent";
	}

	//查询到个人信息后更新
	@RequestMapping(value = "/getself1")
	public String getSelf1( ModelMap modelMap,HttpSession session){


		System.out.println(session.getAttribute("aId"));
		//得强制转化为String
		Student bean = studentService.getById((String) session.getAttribute("aId"));
//          modelMap.addAttribute("adminmsg","bean");
		modelMap.addAttribute("studentmsg",bean);

		return "studentMsg";
	}
	//更新个人信息
	@RequestMapping(value = "/update")
	public String update(Student bean, ModelMap modelMap,HttpSession session){
		System.out.println(bean);

		boolean flag =studentService.update(bean);
		if (flag == true){
			return "forward:/student/getself";
		}else{
			return "studentMsg";
		}

	}


	//查询个人成绩
	@RequestMapping(value = "/getgrade")
	public String getGrade(ModelMap modelMap, HttpSession session){
		System.out.println(session.getAttribute("aId"));
		//得强制转化为String
		List<Grade> bean = gradeService.getById((String) session.getAttribute("aId"));
		modelMap.addAttribute("grademsg",bean);
		return "selectGrade";
	}

	//成绩报错,先去到报错界面，再进行信息提交
	@RequestMapping(value = "/errorgrade")
	public String errorGrade(Grade grade, ModelMap modelMap, HttpServletRequest req) throws UnsupportedEncodingException {
		System.out.println(grade);
		//得强制转化为String
		modelMap.addAttribute("errorgrademsg",grade);
		return "addErrorGrade";
	}
	@RequestMapping(value = "/adderrorgrade")
	public String addErrorGrade(ErrorGrade errorGrade, ModelMap modelMap){
		System.out.println(errorGrade);
		//得强制转化为String
		boolean flag = errorGradeService.addError(errorGrade);
		if(flag == true){
			return "forward:/student/getgrade";
		}
		else{
			modelMap.addAttribute("msg","操作有误，请重新操作");
			return "error";
		}
	}
}
